import requests
import json

# 从配置文件加载配置
with open('config.json', 'r', encoding='utf-8') as config_file:
    config = json.load(config_file)

api_key = config['api_key']
url = config['url']
model = config['model']

headers = {
    'Content-Type': 'application/json',
    'Authorization': f'Bearer {api_key}'
}

#content = '为什么天空是蓝色的？用中文回答'

def get_gemini_response(content):
    data = {
        'model': model,
        'messages': [{'role': 'user', 'content': content}],
    }

    try:
        response = requests.post(url, headers=headers, json=data)
        response.raise_for_status()  # Raises an exception for HTTP errors
        # print(response.json())  # Print the JSON response
        # 输出response.json()中的content
        print(response.json()['choices'][0]['message']['content'])

    except requests.exceptions.RequestException as e:
        print(f"Request failed: {e}")

if __name__ == '__main__':
    while True:
        content = input('请输入要发送给 Gemini 的内容: ')
        get_gemini_response(content)
